﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace luanvanFinal
{
    class reqTrace
    {
        int id;
        String idReq;
        int idPro;
        Nullable<DateTime> startDay = null;
        int effort;
        int completeEffort;
        String assignment;
        int cost;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        public String IdReq
        {
            get { return idReq; }
            set { idReq = value; }
        }
        public int IdPro
        {
            get { return idPro; }
            set { idPro = value; }
        }
        public Nullable<DateTime> StartDay
        {
            get { return startDay; }
            set { startDay = value; }
        }
        public int Effort
        {
            get { return effort; }
            set { effort = value; }
        }
        public int CompleteEffort
        {
            get { return completeEffort; }
            set { completeEffort = value; }
        }
        public String Assignment
        {
            get { return assignment; }
            set { assignment = value; }
        }
        public int Cost
        {
            get { return cost; }
            set { cost = value; }
        }
        public reqTrace()
        {
            id = 0;
            idReq = "";
            idPro = 0;
            startDay = null;
            effort = 0;
            completeEffort = 0;
            assignment = "";
            cost = 0;
        }

        public int chectProAndReq(String pro, String req)
        {
            clsProject project = new clsProject();
            int idProject = Convert.ToInt32(project.getIdFromName(pro));
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            SqlDataAdapter da = new SqlDataAdapter("select * from reqTrace where idReq = '"+req+"' and idPro='"+idProject+"' ", ketnoi.StrConnection);
            DataTable dt = new DataTable();
            da.Fill(dt);
            ketnoi.closeConnection();
            return dt.Rows.Count;
        }

        public void insertReqTrace()
        {
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            try
            {
                SqlCommand sqlcmd = new SqlCommand("insertReqTrace", ketnoi.Connection);
                sqlcmd.CommandType = CommandType.StoredProcedure;
                sqlcmd.Parameters.Add("@idReq", SqlDbType.NVarChar).Value = this.idReq;
                sqlcmd.Parameters.Add("@idPro", SqlDbType.Int).Value = this.idPro;
                sqlcmd.Parameters.Add("@startDay", SqlDbType.DateTime).Value = this.startDay;
                sqlcmd.Parameters.Add("@effort", SqlDbType.Int).Value = this.effort;
                sqlcmd.Parameters.Add("@completeEffort", SqlDbType.Int).Value = this.completeEffort;
                sqlcmd.Parameters.Add("@assignment", SqlDbType.NVarChar).Value = this.assignment;
                sqlcmd.Parameters.Add("@cost", SqlDbType.Int).Value = this.cost;                

                sqlcmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Dữ liệu đã tồn tại hoặc không hợp lệ!!!" + ex.ToString(), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            ketnoi.closeConnection();
        }

        public void updateReqTrace()
        {
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            try
            {
                SqlCommand sqlcmd = new SqlCommand("updateReqTrace", ketnoi.Connection);
                sqlcmd.CommandType = CommandType.StoredProcedure;
                sqlcmd.Parameters.Add("@idReq", SqlDbType.NVarChar).Value = this.idReq;
                sqlcmd.Parameters.Add("@idPro", SqlDbType.Int).Value = this.idPro;
                sqlcmd.Parameters.Add("@startDay", SqlDbType.DateTime).Value = this.startDay;
                sqlcmd.Parameters.Add("@effort", SqlDbType.Int).Value = this.effort;
                sqlcmd.Parameters.Add("@completeEffort", SqlDbType.Int).Value = this.completeEffort;
                sqlcmd.Parameters.Add("@assignment", SqlDbType.NVarChar).Value = this.assignment;
                sqlcmd.Parameters.Add("@cost", SqlDbType.Int).Value = this.cost;

                sqlcmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Dữ liệu đã tồn tại hoặc không hợp lệ!!!" + ex.ToString(), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            ketnoi.closeConnection();
        }
        public void deleteReqTrace()
        {
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            try
            {
                SqlCommand sqlcmd = new SqlCommand("deleteReqTrace", ketnoi.Connection);
                sqlcmd.CommandType = CommandType.StoredProcedure;
                sqlcmd.Parameters.Add("@idReq", SqlDbType.NVarChar).Value = this.idReq;
                sqlcmd.Parameters.Add("@idPro", SqlDbType.Int).Value = this.idPro;
                sqlcmd.ExecuteNonQuery();
                MessageBox.Show("You have delete success !!!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("You have error while deleting project!!!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            ketnoi.closeConnection();
        }

        public void deleteReqTraceByIdProject()
        {
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            try
            {
                SqlCommand sqlcmd = new SqlCommand("deleteReqTraceBiIdProject", ketnoi.Connection);
                sqlcmd.CommandType = CommandType.StoredProcedure;
                //sqlcmd.Parameters.Add("@idReq", SqlDbType.NVarChar).Value = this.idReq;
                sqlcmd.Parameters.Add("@idPro", SqlDbType.Int).Value = this.idPro;
                sqlcmd.ExecuteNonQuery();
                //MessageBox.Show("You have delete success !!!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("You have error while deleting project!!!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            ketnoi.closeConnection();
        }

        public void deleteReqTraceByIdReq()
        {
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            try
            {
                SqlCommand sqlcmd = new SqlCommand("deleteReqTraceByIdReq", ketnoi.Connection);
                sqlcmd.CommandType = CommandType.StoredProcedure;
                //sqlcmd.Parameters.Add("@idReq", SqlDbType.NVarChar).Value = this.idReq;
                sqlcmd.Parameters.Add("@idReq", SqlDbType.NVarChar).Value = this.idReq;
                sqlcmd.ExecuteNonQuery();
                //MessageBox.Show("You have delete success !!!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch
            {
                MessageBox.Show("You have error while deleting project!!!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            ketnoi.closeConnection();
        }
    }
    
    
}
